﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using DataAccess.BusinessEntities;

namespace Locator.PersonalLocations.Views
{
    /// <summary>
    /// 
    /// </summary>
    public class UsersPersonalLocationsPresenter : Presenter<IUsersPersonalLocationsView>, IDisposable
    {
        /// <summary>
        /// 
        /// </summary>
        private readonly IPersonalLocationsController controller;

        /// <summary>
        /// Initializes a new instance of the <see cref="UsersPersonalLocationsPresenter"/> class.
        /// </summary>
        /// <param name="controller">The controller.</param>
        public UsersPersonalLocationsPresenter([CreateNew] IPersonalLocationsController controller)
        {
            this.controller = controller;
        }

        /// <summary>
        /// Called when on every view load.
        /// </summary>
        public override void OnViewLoaded()
        {
            var locations = this.controller.GetPersonalLocationsForCurrentUser();
            this.View.SetPersonalLocations(locations);
        }

        /// <summary>
        /// Called when the view is first loaded initialized.
        /// </summary>
        public override void OnViewInitialized()
        {
        }

        /// <summary>
        /// Called when the Add Personal Locations link is clicked.
        /// </summary>
        public void OnAddPersonalLocationsClicked()
        {
            this.controller.AddPersonalLocations();
        }

        /// <summary>
        /// Called when a personal location is deleted.
        /// </summary>
        /// <param name="personalLocation">The personal location.</param>
        public void OnPersonalLocationDeleted(PersonalLocation personalLocation)
        {
            this.controller.DeletePersonalLocationForCurrentUser(personalLocation.Id);
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.controller != null)
                {
                    this.controller.Dispose();
                }
            }
        }

        ~UsersPersonalLocationsPresenter()
        {
            Dispose(false);
        }
    }
}




